#ifndef _KZLIB_ADS1258_
#define _KZLIB_ADS1258_

#include "../common_typedef.h"

constexpr uint8_t ADC1258_ID_VALID = 0x8b;
constexpr uint8_t ADC1258_TIMEOUT_LIMIT = 10; // ms
constexpr uint8_t ADC1258_CHNL_MAX = 16;

enum ADC_CHIPSTA {
    ADC_CHIP_CS_COMFAIL,
    ADC_CHIP_WRONG_ID,
    ADC_CHIP_RDY,
};

struct Ads1258Result {
    int32_t adVal[ADC1258_CHNL_MAX] = 0;
    int32_t state[ADC1258_CHNL_MAX] = 0;
};


class DrvAds1258 {
public:
    DrvAds1258(
        uint32_t dataBus, uint32_t ioCs, uint32_t ioStart, uint32_t ioRdy,
        void    (*delayus)  (uint32_t),
        uint32_t slot, uint32_t chipIdx
    ) : m_dataBus(dataBus), m_ioCs(ioCs), m_ioStart(ioStart), m_ioRdy(ioRdy),
        m_delayus(delayus),
        m_slot(slot), m_chipIdx(chipIdx)
    {

    }
    void Init();
    void ReadState();
    void ReadChipResult(uint8_t ch, Ads1258Result& result);

private:
    uint32_t m_dataBus = 0xffffffff;
    uint32_t m_ioCs = 0xffffffff;
    uint32_t m_ioStart = 0xffffffff;
    uint32_t m_ioRdy = 0xffffffff;
    void    (*m_delayus)  (uint32_t);
    uint32_t m_slot = 0;
    uint32_t m_chipIdx = 0;
    uint32_t m_sta = ADC_CHIP_CS_COMFAIL;
    uint32_t adcVal[ADC1258_CHNL_MAX] = {0};
};

#endif